package com.example.demo2.controller;

import com.example.demo2.bean.AdminBean;

import com.example.demo2.service.AdminBeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
@CrossOrigin(origins = "*",
        methods = {RequestMethod.GET,RequestMethod.OPTIONS,RequestMethod.POST})
public class AdminBeanController {

    @Autowired
    AdminBeanService adminBeanService;

    // 登录
    @RequestMapping("/admin/login")
    public Map<String, Object> getMember(@RequestBody AdminBean adminBean) {
        Map<String, Object> map = new HashMap<>();
        AdminBean adminBean1 = adminBeanService.login(adminBean);
        if (adminBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

    // 注册
    @RequestMapping("/admin/register")
    public Map<String, Object> addMember(@RequestBody AdminBean adminBean) {
        Map<String, Object> map = new HashMap<>();
        AdminBean adminBean1 = adminBeanService.register(adminBean);
        if (adminBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

    // 查询一个
    @RequestMapping("/admin/findone")
    public Map<String, Object> findone(@RequestBody AdminBean adminBean) {
        Map<String, Object> map = new HashMap<>();
        AdminBean adminBean1 = adminBeanService.findone(adminBean);
        if (adminBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", adminBean1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

    // 查询所有
    @RequestMapping("/admin/list")
    public Map<String, Object> list() {
        Map<String, Object> map = new HashMap<>();
        List<AdminBean> list = adminBeanService.list();
        if (list != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("list", list);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

    // 删除
    @RequestMapping("/admin/delete")
    public Map<String, Object> delete(@RequestBody AdminBean adminBean) {
        Map<String, Object> map = new HashMap<>();
        try {
            adminBeanService.deleteById(adminBean.getAdmin_id());
            map.put("success", true);
            map.put("msg", "成功");
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", "失败");
        }

        return map;
    }
    // 修改
    @RequestMapping("/admin/update")
    public Map<String, Object> update(@RequestBody AdminBean adminBean) {
        Map<String, Object> map = new HashMap<>();
        AdminBean adminBean1 = adminBeanService.update(adminBean);
        if (adminBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", adminBean1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
    // 添加
    @RequestMapping("/admin/addone")
    public Map<String, Object> addone(@RequestBody AdminBean adminBean) {
        Map<String, Object> map = new HashMap<>();
        AdminBean adminBean1 = adminBeanService.addone(adminBean);
        if (adminBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", adminBean1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

}
